class Solution {//leetcode1219——黄金矿工
private:
    int dx[4]={1,-1,0,0};
    int dy[4]={0,0,1,-1};
    void dfs(vector<vector<int>>& grid,int gold,int row,int col){
        if(gold>ret) ret=gold;
        for(int i=0;i<4;i++){
            int x=col+dx[i],y=row+dy[i];
            if(x>=0&&x<n&&y>=0&&y<m&&grid[y][x]!=0&&used[y][x]==false){
                used[y][x]=true;
                dfs(grid,gold+grid[y][x],y,x);
                used[y][x]=false;
            }
        }
    }
public:
    int getMaximumGold(vector<vector<int>>& grid) {
        m=grid.size();
        n=grid[0].size();
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++){
                if(grid[i][j]!=0){
                    used[i][j]=true;
                    dfs(grid,grid[i][j],i,j);
                    used[i][j]=false;
                }
            }
        }
        return ret;
    }
private:
    int ret=0;
    int m;
    int n;
    bool used[15][15]={false};
};